﻿using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public interface ILevelUpSystem : ISystem
    {
        List<LevelUpItemData> LevelUpItemDatas { get; }
    }
    
    public class LevelUpSystem : AbstractSystem,ILevelUpSystem
    {
        public List<LevelUpItemData> LevelUpItemDatas { get; private set; }
        
        protected override void OnInit()
        {
            LevelUpItemDatas = new List<LevelUpItemData>();

            var toolBarSystem = this.GetSystem<IToolbarSystem>();
            var saveUtility = this.GetUtility<ISaveUtility>();
            
            var level1 = new LevelUpItemData()
                .WithLevel(1)
                .WithUnLock(() => true)
                .WithCondition(() =>
                {
                    if (Global.PotatoCount.Value >= 20)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Potato", 20);
                    Global.PotatoCount.Value -= 20;

                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 50;
                    
                    MessagePanel.Default.AddMessage("等级升为2,体力增长5,获得50金币");
                })
                .AddLevelItemData("Potato", 20);

            var level2 = new LevelUpItemData()
                .WithLevel(2)
                .WithUnLock(() =>
                {
                    if (level1.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.PotatoCount.Value >= 20 && Global.TurnipCount.Value >= 15)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Potato", 20);
                    toolBarSystem.SubToolbarData("Turnip", 15);
                    Global.PotatoCount.Value -= 20;
                    Global.TurnipCount.Value -= 15;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 50;
                    MessagePanel.Default.AddMessage("等级升为3,体力增长5,获得50金币");
                })
                .AddLevelItemData("Potato", 20)
                .AddLevelItemData("Turnip", 15);

            var level3 = new LevelUpItemData()
                .WithLevel(3)
                .WithUnLock(() =>
                {
                    if (level2.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.PotatoCount.Value >= 35 && Global.TurnipCount.Value >= 15 && Global.PinkRadishCount.Value >= 10)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Potato", 35);
                    toolBarSystem.SubToolbarData("Turnip", 15);
                    toolBarSystem.SubToolbarData("PinkRadish", 10);

                    Global.PotatoCount.Value -= 35;
                    Global.TurnipCount.Value -= 15;
                    Global.PinkRadishCount.Value -= 10;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 100;
                    MessagePanel.Default.AddMessage("等级升为4,体力增长5,获得100金币");
                    
                })
                .AddLevelItemData("Potato", 35)
                .AddLevelItemData("Turnip", 15)
                .AddLevelItemData("PinkRadish", 10);

            var level4 = new LevelUpItemData()
                .WithLevel(4)
                .WithUnLock(() =>
                {
                    if (level3.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.PinkRadishCount.Value >= 40)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("PinkRadish", 40);
                    Global.PinkRadishCount.Value -= 40;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 100;
                    MessagePanel.Default.AddMessage("等级升为5,体力增长5,获得100金币");
                })
                .AddLevelItemData("PinkRadish", 40);

            var level5 = new LevelUpItemData()
                .WithLevel(5)
                .WithUnLock(() =>
                {
                    if (level4.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.PotatoCount.Value >= 20 && Global.TurnipCount.Value >= 20 && Global.PinkRadishCount.Value >= 15)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Potato", 20);
                    toolBarSystem.SubToolbarData("Turnip", 20);
                    toolBarSystem.SubToolbarData("PinkRadish", 15);
                    
                    Global.PotatoCount.Value -= 20;
                    Global.TurnipCount.Value -= 20;
                    Global.PinkRadishCount.Value -= 15;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 150;
                    MessagePanel.Default.AddMessage("等级升为6,体力增长5,获得150金币");
                })
                .AddLevelItemData("Potato", 20)
                .AddLevelItemData("Turnip", 20)
                .AddLevelItemData("PinkRadish", 15);

            var level6 = new LevelUpItemData()
                .WithLevel(6)
                .WithUnLock(() =>
                {
                    if (level5.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.GrapeCount.Value >= 25 && Global.BlueBerryCount.Value >= 25)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Grape", 25);
                    toolBarSystem.SubToolbarData("BlueBerry", 25);

                    Global.GrapeCount.Value -= 25;
                    Global.BlueBerryCount.Value -= 25;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 150;
                    MessagePanel.Default.AddMessage("等级升为7,体力增长5,获得150金币");
                })
                .AddLevelItemData("Grape", 25)
                .AddLevelItemData("BlueBerry", 25);

            var level7 = new LevelUpItemData()
                .WithLevel(7)
                .WithUnLock(() =>
                {
                    if (level6.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.GrapeCount.Value >= 20 && Global.BlueBerryCount.Value >= 20 && Global.EggplantCount.Value >= 5)
                    {
                        return true;
                    }
                    
                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Grape", 20);
                    toolBarSystem.SubToolbarData("BlueBerry", 20);
                    toolBarSystem.SubToolbarData("Eggplant", 5);

                    Global.GrapeCount.Value -= 20;
                    Global.BlueBerryCount.Value -= 20;
                    Global.EggplantCount.Value -= 5;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 200;
                    MessagePanel.Default.AddMessage("等级升为8,体力增长5,获得200金币");
                })
                .AddLevelItemData("Grape", 20)
                .AddLevelItemData("BlueBerry", 20)
                .AddLevelItemData("Eggplant", 5);

            var level8 = new LevelUpItemData()
                .WithLevel(8)
                .WithUnLock(() =>
                {
                    if (level7.IsUp)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.EggplantCount.Value >= 25)
                    {
                        return true;
                    }

                    return false;
                })
                .WithOnFinished(() =>
                {
                    toolBarSystem.SubToolbarData("Eggplant", 25);
                    Global.EggplantCount.Value -= 25;
                    
                    Global.BrawnConfig += 5;
                    Global.Brawn.Value = Global.BrawnConfig;
                    Global.Coin.Value += 250;
                    MessagePanel.Default.AddMessage("完成升级,体力增长5,获得250金币");
                })
                .AddLevelItemData("Eggplant", 25);

            AddData(level1)
                .AddData(level2)
                .AddData(level3)
                .AddData(level4)
                .AddData(level5)
                .AddData(level6)
                .AddData(level7)
                .AddData(level8);

            var currentLevel = saveUtility.LoadInt("CurrentLevel", 1);

            foreach (var levelUpItemData in LevelUpItemDatas)
            {
                if (levelUpItemData.Level == currentLevel)
                {
                    break;
                }
                levelUpItemData.WithUnLock(() => true);
                levelUpItemData.IsUp = true;
            }
            
            
            saveUtility.AddSaveDataAction(utility =>
            {
                var currentLevel = LevelUpItemDatas.Find(data => data.mUnlock() && !data.IsUp).Level;
                utility.SaveInt("CurrentLevel",currentLevel);
            });

        }

        LevelUpSystem AddData(LevelUpItemData data)
        {
            LevelUpItemDatas.Add(data);
            return this;
        }
    }
}